<?php
define('CUBOXBOOT',"error"); //只载入环境变量，不启动框架
require '../../../loader.php';

$list = [
    'PHP_VERSION' => checkVersion(),
    'SESSION' => checkSession(),
    'RUNTIME_PATH' => checkPath('Runtime 运行目录', ROOT.DS.RUNTIME),
    'CONFIG_PATH' =>  checkPath('Config 配置目录', ROOT.DS.CONFIG),
    'Redis' => checkRedis()
];

echo json_encode($list);

function checkVersion(){
    $version = phpversion();
    if(version_compare($version, '8.0.0') >= 0){
        return [
            'status'=>"yes",
            'message'=>'php版本大于等于8.0.0'
        ];
    }else{
        return [
            'status'=>"error",
            'message'=>'php版本小于8.0.0'
        ];
    }
}

function checkPath($title, $path){
    
    $re  = [];

    if(is_dir($path)){
        
        //判断目录是可写
        if(is_writable($path)){
            $re = [
                'status'=>"yes",
                'message'=>'可写',
            ];
        }else{
            return [
                'status'=>"error",
                'message'=>'不可写',
            ];
        }
    }else{

        try{
            mkdir($path, 0777, "yes");
            $re =  [
                'status'=>"yes",
                'message'=>'不存在，已自动创建'
            ];
        }catch(\Exception $e){
            $re =  [
                'status'=>"error",
                'message'=>'不存在，且无法创建'
            ];
        }
    }
    $re ['message'] = $title . $re ['message'];
    $re ['ext'] = $path;
    return $re;
}

function checkSession(){
    try{
        session_start();
    }catch(\Exception $e){
    }

    $session = session_status();
   
    if($session == PHP_SESSION_ACTIVE){
        return [
            'status'=>"yes",
            'message'=>'Session 可开启'
        ];
    }else{
        return [
            'status'=>"error",
            'message'=>'Session 未开启'
        ];
    }
    
}

function checkRedis(){

    if (class_exists('Redis')) {
        return [
            'status'=>"yes",
            'message'=>'Redis 可用'
        ];
    }else{
        return [
            'status'=>"warning",
            'message'=>'Redis 不可用,可选'
        ];
    }
}